package com.ruoyi.tws.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.Condition;
import com.ruoyi.common.core.web.page.Query;
import com.ruoyi.tws.api.domain.Order;
import com.ruoyi.tws.domain.OrderMonetaryChange;
import com.ruoyi.tws.domain.OrderSecure;
import com.ruoyi.tws.service.IOrderMonetaryChangeService;
import com.ruoyi.tws.service.IOrderSecureService;
import com.ruoyi.tws.service.IOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 手机端 - 订单controller
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping(Constants.APP_URL + "/")
public class AppOrderController extends BaseController {

    private final IOrderService orderService;
    private final IOrderSecureService iOrderSecureService;
    private final IOrderMonetaryChangeService orderMonetaryChangeService;

    /**
     * 订单列表
     * @param
     * @return
     */
    @GetMapping("orderList")
    public R<IPage<Order>> orderList(Query query, @RequestParam String orderStatus) {

        LambdaQueryWrapper<Order> lqw = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(orderStatus)) {
            lqw.eq(Order::getStatus, orderStatus);
        }
        lqw.eq(Order::getDriverId, SecurityUtils.getUserId());
        lqw.orderByDesc(Order::getCreateTime);
        IPage<Order> list = orderService.page(Condition.getPage(query), lqw);
        return R.ok(list);
    }

    /**
     * 订单按状态计数
     * @param
     * @return
     */
    @GetMapping("orderCount")
    public R<List<Map<String, Object>>> orderCount() {
        QueryWrapper<Order> lqw = new QueryWrapper<>();
        lqw.select("count(id) AS num", "status")
                .eq("driver_id", SecurityUtils.getUserId())
                .groupBy("status");
        List<Map<String, Object>> maps = orderService.listMaps(lqw);
        return R.ok(maps);
    }

    /**
     * 详情
     * @param orderId
     * @return
     */
    @GetMapping("orderDetail")
    public R<Order> getOrderDetail(@RequestParam("orderId") Long orderId) {
        return R.ok(orderService.getInfo(orderId));
    }

    /**
     * 发车
     * @param order
     * @return
     */
    @PostMapping("startOrder")
    public R startOrder(@RequestBody Order order) {
        return orderService.appStartOrder(order) ? R.ok(): R.fail();
    }

    /**
     * 回单
     * @param order
     * @return
     */
    @PostMapping("backOrder")
    public R backOrder(@RequestBody Order order) {
        return orderService.appBackOrder(order) ? R.ok() : R.fail();
    }

    /**
     * 评价
     * @param order
     * @return
     */
    @PostMapping("starOrder")
    public R starOrder(@RequestBody Order order) {
        return orderService.starOrder(order) ? R.ok() : R.fail();
    }

    /**
     * 添加解除申请
     * @param orderSecure
     * @return
     */
    @Transactional
    @PostMapping("secure")
    public AjaxResult secureAdd(@RequestBody OrderSecure orderSecure){
        Order order = new Order();
        order.setId(orderSecure.getOrderId());
        order.setStatus("5");
        orderService.updateById(order);
        return toAjax(iOrderSecureService.save(orderSecure)? 1 : 0);
    }

    /**
     * 申请解除订单列表
     * @param
     * @return
     */
    @GetMapping("secureList")
    public R<IPage<OrderSecure>> secureList(Query query) {

        LambdaQueryWrapper<OrderSecure> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OrderSecure::getCreateBy, SecurityUtils.getUserId());
        lqw.orderByDesc(OrderSecure::getCreateTime);
        IPage<OrderSecure> list = iOrderSecureService.page(Condition.getPage(query), lqw);
        return R.ok(list);
    }

    /**
     * 添加申请
     * @param orderMonetaryChange
     * @return
     */
    @Transactional
    @PostMapping("change")
    public AjaxResult changeAdd(@RequestBody OrderMonetaryChange orderMonetaryChange){
        Order order = new Order();
        order.setId(orderMonetaryChange.getOrderId());
        order.setStatus("5");
        orderService.updateById(order);
        return toAjax(orderMonetaryChangeService.save(orderMonetaryChange)? 1 : 0);
    }

    /**
     * 申请解除订单列表
     * @param
     * @return
     */
    @GetMapping("monetaryChangeList")
    public R<IPage<OrderMonetaryChange>> changeList(Query query) {

        LambdaQueryWrapper<OrderMonetaryChange> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OrderMonetaryChange::getCreateBy, SecurityUtils.getUserId());
        lqw.orderByDesc(OrderMonetaryChange::getCreateTime);
        IPage<OrderMonetaryChange> list = orderMonetaryChangeService.page(Condition.getPage(query), lqw);
        return R.ok(list);
    }
}
